<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="title">用户登录</h2>
      <el-form :model="form" ref="form">
        <el-form-item style="display: flex;justify-content: center;">
          <!--          label="账号" :rules="[{ required: true, message: '请输入账号', trigger: 'blur' }]"-->
          <el-input v-model="form.username" placeholder="请输入账号" style="width: 280px;"></el-input>
        </el-form-item>

        <el-form-item style="display: flex;justify-content: center;">
          <!--          label="密码" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"-->
          <el-input v-model="form.password" type="password" placeholder="请输入密码" style="width: 280px"></el-input>
        </el-form-item>

        <el-form-item style="display: flex;justify-content: center;">
          <el-button type="primary" @click="handleLogin" :loading="loading" round>登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';


export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      loading: false
    };
  },
  methods: {
    handleLogin() {
      if (!this.form.username || !this.form.password) {
        this.$message.error('账号和密码不能为空');
        return;
      }

      this.loading = true;

      // 发起登录请求
      axios.post('http://localhost:3000/auth/login', this.form, {
        headers: {
          'Content-Type': 'application/json'
        }
      })
          .then(response => {
            if (response.data.code === 0) {
              this.$message.success('登录成功');
              // localStorage.setItem('token', response.data.token);  // 存储 JWT token
              const userInfo = {
                token: response.data.token,
                id: response.data.id
              };

              // 存储到 localStorage
              localStorage.setItem('userInfo', JSON.stringify(userInfo));

              this.$router.push('/');
            } else {
              this.$message.error(response.data.message || '登录失败');
            }
          })
          .catch(error => {
            console.error(error);
            if (error.response && error.response.status === 401) {
              this.$message.error(error.response.data.message || '账号或密码错误');
            } else {
              this.$message.error('登录失败，请稍后再试');
            }
          })
          .finally(() => {
            this.loading = false;
          });
    },

  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/img/登录背景图.jpg'); /* 设置背景图 */
  background-size: cover; /* 背景图自适应填满屏幕 */
  background-position: center; /* 背景图居中 */
  background-repeat: no-repeat; /* 防止背景图重复 */
}

.login-card {
  width: 400px;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.8);
}

.title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  color: #409EFF;
}

.el-form-item {
  margin-bottom: 20px;
}
</style>
